﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace WasaWasa
{
    /// <summary>
    /// PictogramPaletteWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class PictogramPaletteWindow : Window
    {
        public event EventHandler<ObjectEventArgs<int>> PictogramInputed;

        public PictogramPaletteWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (UIElement elm in EnumPictograms())
            {
                if (elm == null) continue;
                lstPictograms.Items.Add(elm);
            }
        }

        private IEnumerable<Image> EnumPictograms()
        {
            foreach (int i in EnumPictogramCharcodes())
            {
                yield return LoadPictogram(i);
            }
        }

        private IEnumerable<int> EnumPictogramCharcodes()
        {
            for (int i = 0xE001; i < 0xE05A; i++)
                yield return i;
            for (int i = 0xE101; i < 0xE15A; i++)
                yield return i;
            for (int i = 0xE201; i < 0xE257; i++)
                yield return i;
            for (int i = 0xE301; i < 0xE34D; i++)
                yield return i;
            for (int i = 0xE401; i < 0xE44C; i++)
                yield return i;
            for (int i = 0xE468; i < 0xE5DF; i++)
                yield return i;
            for (int i = 0xE63E; i < 0xE757; i++)
                yield return i;
            for (int i = 0xEA80; i < 0xEB88; i++)
                yield return i;
            for (int i = 0xEC40; i < 0xECFC; i++)
                yield return i;
            for (int i = 0xED40; i < 0xED80; i++)
                yield return i;
        }

        private Image LoadPictogram(int i)
        {
            try
            {
                BitmapImage img = new BitmapImage(new Uri(string.Format("pack://application:,,,/WasaWasa.Pictograms;component/{0:X}.gif", i)));
                Image imgctrl = new Image()
                {
                    Source = img,
                    Width = img.PixelWidth,
                    Height = img.PixelHeight,
                };
                imgctrl.Tag = i;
                return imgctrl;
            }
            catch (IOException)
            {
                return null;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (PictogramInputed != null)
            {
                PictogramInputed(this, new ObjectEventArgs<int>((int)(sender as Image).Tag));
            }
        }

        private void lstPictograms_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (lstPictograms.SelectedItem != null && PictogramInputed != null)
            {
                PictogramInputed(this, new ObjectEventArgs<int>((int)(lstPictograms.SelectedItem as Image).Tag));
            }
        }

        private void lstPictograms_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.Key == Key.Space || e.Key == Key.Enter))
                return;
            if (lstPictograms.SelectedItem != null && PictogramInputed != null)
            {
                PictogramInputed(this, new ObjectEventArgs<int>((int)(lstPictograms.SelectedItem as Image).Tag));
            }
        }
    }

    public class ObjectEventArgs<T> : EventArgs
    {
        public T Value { get; set; }

        public ObjectEventArgs(T value)
        {
            Value = value;
        }
    }
}
